package com.wing.cms.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.wing.cms.domain.Channel;
import com.wing.common.CommonSpringJDBC;
import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;

@Repository
public class ChannelDao extends CommonSpringJDBC<Channel, String> {

	public PageBean findPageList(int currentPage, int pageSize, Object... args) {
		String where = " cc.pid='" + args[1] + "' and  cc.site_id='" + args[0]
				+ "' ";
		String sql1 = " SELECT cc.*,clf.name clfname,cl.name clname FROM cms_channel cc left join cms_lookfeel clf on cc.lookandfeel=clf.id "
				+ " left join cms_layout cl on cc.layout = cl.id "
				+ " where "
				+ where
				+ " ORDER BY cc.order_num ASC "
				+ " LIMIT "
				+ (currentPage - 1) * pageSize + "," + pageSize;
		String sql2 = "select count(cc.id) from  cms_channel cc left join cms_lookfeel clf on cc.lookandfeel=clf.id "
				+ " left join cms_layout cl on cc.layout = cl.id where" + where;
		String[] sqlArray = { sql1, sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public List<Map<String, Object>> findChildByID(String id, String siteId) {
		String sql = "select t.* from cms_channel t where t.pid=? and t.site_id=? order by order_num";
		Object[] objs = { id, siteId };
		return super.getSimpleJdbcTemplate().queryForList(sql, objs);
	}

	public List<Map<String, Object>> findChannelsBySiteId(String siteId) {
		String sql = "select t.* from cms_channel t where t.site_id=? order by order_num";
		return super.getSimpleJdbcTemplate().queryForList(sql, siteId);
	}

	public PageBean findPageListForChannelArt(int currentPage, int pageSize,
			Object... args) {
		String where = " a.id=ac.article_id and ac.channel_id='" + args[0]
				+ "' ";
		String sql1 = " SELECT ac.id as acid,a.* FROM cms_article a,cms_article_channel ac where "
				+ where
				+ " ORDER BY a.id ASC "
				+ " LIMIT "
				+ (currentPage - 1)
				* pageSize + "," + pageSize;
		String sql2 = "select count(a.id) FROM cms_article a,cms_article_channel ac where"
				+ where;
		String[] sqlArray = { sql1, sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public PageBean findPageListForArt(int currentPage, int pageSize) {
		String where = " 1=1 ";
		String sql1 = " SELECT a.* FROM cms_article a where " + where
				+ " ORDER BY a.id ASC " + " LIMIT " + (currentPage - 1)
				* pageSize + "," + pageSize;
		String sql2 = "select count(a.id) FROM cms_article a where" + where;
		String[] sqlArray = { sql1, sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public List<Map<String, Object>> findChildByID(String chanId) {
		String sql = "select t.* from cms_channel t where t.pid=? order by order_num";
		Object[] objs = { chanId };
		return super.getSimpleJdbcTemplate().queryForList(sql, objs);
	}

	public String findLayoutFileById(String chanId) {
		String sql = "select l.filename from cms_channel t,cms_layout l where t.layout = l.id and t.id = ?";
		Object[] objs = { chanId };
		return (String) super.getSimpleJdbcTemplate().queryForObject(sql,
				String.class, objs);
	}
	
	public String findSitePathById(String chanId) {
		String sql = "select l.site_path from cms_channel t,cms_site l where t.site_id = l.id and t.id = ?";
		Object[] objs = { chanId };
		return (String) super.getSimpleJdbcTemplate().queryForObject(sql,
				String.class, objs);
	}

	// 复制频道
	public void cloneChannel(String id) {
		Channel source = super.findById(id);
		String newId  =  new GUID().toString();;
		String cppstr=generatCppStr();
		
		String insert_$cms_channel$_sql = "insert into cms_channel values('"+newId+"','"+source.getPid()
		+"','"+source.getSite_id()+"','复制"+source.getChannel_name()+"','"+source.getLookandfeel()+"','"+source.getLayout()
		+"','"+source.getOrder_num()+"','"+source.getValid_flag()+"','"
		+source.getRemarks()+"','"+source.getStatus()+"','"+source.getIspub()+"')";
		
		String insert_$cms_placeholder$_sql =  "insert into cms_placeholder (select concat('"+cppstr+"',id), placeholderid ,'"+ newId
		+"', concat('"+cppstr+"',portletId),layoutId ,portletype ,order_num from cms_placeholder where channelid = '"+id+"')";
		
		String insert_$cms_portletinstance$_sql = "insert into cms_portletinstance (select concat('"+cppstr+"',id),artgroupId,isnavi,showlevel," +
				"assoportlet" +",showstate,titlelength,articlecount,summarylength,templateid,title,portletwidth,portletheight,titlecolor," +
				"titlepic,titletype from cms_portletinstance where id in (select portletId from cms_placeholder where channelid = '"+id+"'))";
		
		String insert_$cms_portletinstance1$_sql = "insert into cms_portletinstance1 (select concat('"+cppstr+"',id),menuchannelId,showlevel," +
		"isopened,showstate,templateid,title,titlecolor,portletwidth,portletheight," +
		"titlepic,titletype from cms_portletinstance1 where id in (select portletId from cms_placeholder where channelid = '"+id+"'))";

		String insert_$cms_portletinstance2$_sql = "insert into cms_portletinstance2 (select concat('"+cppstr+"',id),productgroupId" +
				",isnavi,showlevel,assoportlet,showstate,titlelength,productcount,summarylength,templateid,title,portletwidth,portletheight," +
		"titlecolor,titlepic,titletype from cms_portletinstance2 where id in (select portletId from cms_placeholder where channelid = '"+id+"'))";
		
		String insert_$cms_portletinstance3$_sql = "insert into cms_portletinstance3 (select concat('"+cppstr+"',id),showcontent" +
		",contentlength,contentsytle,cssname,templateid,title,titlecolor,showstate,portletwidth,portletheight," +
		"titlepic,titletype from cms_portletinstance3 where id in (select portletId from cms_placeholder where channelid = '"+id+"'))";

		String insert_$cms_portletinstance4$_sql = "insert into cms_portletinstance4 (select concat('"+cppstr+"',id),picpath" +
		",picwidth,picheight,picsytle,cssname,templateid,title,titlecolor,showstate,portletwidth,portletheight," +
		"titlepic,titletype from cms_portletinstance4 where id in (select portletId from cms_placeholder where channelid = '"+id+"'))";

		String insert_$cms_portletinstance_c$_sql = "insert into cms_portletinstance_c (select concat('"+cppstr+"',id),templateid,title,titlecolor," +
				"portletwidth,portletheight,titlepic,titletype,object,objecttype" +
				" from cms_portletinstance_c where id in (select portletId from cms_placeholder where channelid = '"+id+"'))";
		String[] sqls = new String[8];
		sqls[0] = insert_$cms_channel$_sql;
		sqls[1] = insert_$cms_placeholder$_sql;
		sqls[2] = insert_$cms_portletinstance$_sql;
		sqls[3] = insert_$cms_portletinstance1$_sql;
		sqls[4] = insert_$cms_portletinstance2$_sql;
		sqls[5] = insert_$cms_portletinstance3$_sql;
		sqls[6] = insert_$cms_portletinstance4$_sql;
		sqls[7] = insert_$cms_portletinstance_c$_sql;
		this.getSimpleJdbcTemplate().getJdbcOperations().batchUpdate(sqls);
	}
	private String generatCppStr(){
		String str="";
		for(int i=0;i<3;i++){
			str+=((int)(Math.random()*9)+1);
		}
		return str;
	}
	
	public void uptemp(String id) {
		Channel c = this.findById(id);
		String sql = "update cms_placeholder set layoutId='"+c.getLayout()+"' where channelid='"+id+"'";
		System.out.println(sql);
		this.getSimpleJdbcTemplate().update(sql);
	}
	
}
